package ru.spbu.math.seabattle.client.jms.gui;

import java.awt.Container;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class FormComponent implements IComponent {
	
	private static final Rectangle URL_LABEL_RECTANGLE =
		new Rectangle(0, 0, 160, 20);
	private static final String URL_LABEL_STRING =
		"URL or IP of a game server:";
	private static final Rectangle NAME_LABEL_RECTANGLE =
		new Rectangle(0, 30, 160, 20);
	private static final String NAME_LABEL_STRING =
		"Your name on this server:";
	private static final Rectangle URL_TEXT_FIELD_RECTANGLE =
		new Rectangle(160, 0, 150, 20);
	private static final Rectangle NAME_TEXT_FIELD_RECTANGLE =
		new Rectangle(160, 30, 150, 20);
	private static final Rectangle BUTTON_RECTANGLE =
		new Rectangle(210, 60, 100, 20);
	private static final String BUTTON_STRING =
		"Connect!";
	private static final String IN_CONNECTION_STRING = 
		"Connecting...";
	private static final Rectangle IN_CONNECTION_RECTANGLE = 
		new Rectangle(100, 70, 100, 20);
	
	
	private JLabel 		serverURLLabel;
	private JLabel 		nameLabel;
	private JTextField 	serverURLTextField;
	private JTextField 	nameTextField;		
	private JButton 	connectButton;
	
	private JLabel inConnectionLabel;
	
	private LoginFormGUI gui;

	private class ConnectListener implements MouseListener {

		@Override
		public void mouseClicked(MouseEvent arg0) {
			gui.connect(
					serverURLTextField.getText(), 
					nameTextField.getText()
				);			
		}

		@Override public void mouseEntered(MouseEvent arg0) {}
		@Override public void mouseExited(MouseEvent arg0) {}
		@Override public void mousePressed(MouseEvent arg0) {}
		@Override public void mouseReleased(MouseEvent arg0) {}
		
	}
	
	public FormComponent(LoginFormGUI gui, String url) {
		
		this.gui = gui;
		
		serverURLLabel = new JLabel(URL_LABEL_STRING);
		serverURLLabel.setBounds(URL_LABEL_RECTANGLE);
		serverURLTextField = new JTextField(url);
		serverURLTextField.setBounds(URL_TEXT_FIELD_RECTANGLE);
		
		nameLabel = new JLabel(NAME_LABEL_STRING);
		nameLabel.setBounds(NAME_LABEL_RECTANGLE);
		nameTextField = new JTextField();
		nameTextField.setBounds(NAME_TEXT_FIELD_RECTANGLE);
		
		connectButton = new JButton(BUTTON_STRING);
		connectButton.setBounds(BUTTON_RECTANGLE);
		connectButton.addMouseListener(new ConnectListener());
		
		inConnectionLabel = new JLabel(IN_CONNECTION_STRING);
		inConnectionLabel.setBounds(IN_CONNECTION_RECTANGLE);
		inConnectionLabel.setVisible(false);
	}
	
	@Override
	public void addToContainer(Container container) {
		
		container.add(serverURLLabel);
		container.add(serverURLTextField);
		container.add(nameLabel);
		container.add(nameTextField);
		container.add(connectButton);
		container.add(inConnectionLabel);
	}

	@Override
	public void setBounds(Rectangle r) {
		Rectangle urlLabelRecangle = 
			new Rectangle(
					URL_LABEL_RECTANGLE.x + r.x,
					URL_LABEL_RECTANGLE.y + r.y,
					URL_LABEL_RECTANGLE.width,
					URL_LABEL_RECTANGLE.height
				);
		
		serverURLLabel.setBounds(urlLabelRecangle);
		
		Rectangle urlTextFieldRecangle = 
			new Rectangle(
					URL_TEXT_FIELD_RECTANGLE.x + r.x,
					URL_TEXT_FIELD_RECTANGLE.y + r.y,
					URL_TEXT_FIELD_RECTANGLE.width,
					URL_TEXT_FIELD_RECTANGLE.height
				);
		
		serverURLTextField.setBounds(urlTextFieldRecangle);
		
		Rectangle nameLabelRectangle =
			new Rectangle(
					NAME_LABEL_RECTANGLE.x + r.x,
					NAME_LABEL_RECTANGLE.y + r.y,
					NAME_LABEL_RECTANGLE.width,
					NAME_LABEL_RECTANGLE.height
				);
		
		nameLabel.setBounds(nameLabelRectangle);
		
		Rectangle nameTextFieldRectangle =
			new Rectangle(
					NAME_TEXT_FIELD_RECTANGLE.x + r.x,
					NAME_TEXT_FIELD_RECTANGLE.y + r.y,
					NAME_TEXT_FIELD_RECTANGLE.width,
					NAME_TEXT_FIELD_RECTANGLE.height
				);
		
		nameTextField.setBounds(nameTextFieldRectangle);
		
		Rectangle buttonRectangle =
			new Rectangle(
					BUTTON_RECTANGLE.x + r.x,
					BUTTON_RECTANGLE.y + r.y,
					BUTTON_RECTANGLE.width,
					BUTTON_RECTANGLE.height
				);
		
		connectButton.setBounds(buttonRectangle);
	}		
	
	public void setEnabled(boolean enabled) {
		serverURLTextField.setEnabled(enabled);
		nameTextField.setEnabled(enabled);
		connectButton.setEnabled(enabled);
	}
	
	public void setInConnection(boolean inConnection) {
		setEnabled(!inConnection);
		showConnecting(inConnection);
	}

	private void showConnecting(boolean inConnection) {
		inConnectionLabel.setVisible(inConnection);		
	}
}
